
****FIRST 25 PORTFOLIOS AND LAST COLUMN (EVERYTHING BUT THE LAST ROW)***
est clear

use "$jfqa_rep/20_Stock_Setup.dta", clear

qui{
foreach i in ret_5 futret_5 five_day_convex_split {
	drop if missing(`i')
}

drop if year<1963
drop if year==1963 & month<6

drop if year>2022
drop if year==2022 & month==12

*convexity split rank
gen convex_five=.
sort monthyear five_day_convex_split
by monthyear: replace convex_five=1 if _n<_N/5
by monthyear: replace convex_five=2 if _n>=_N/5 & _n<_N/5*2
by monthyear: replace convex_five=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace convex_five=4 if _n>=_N/5*3 & _n<_N/5*4
by monthyear: replace convex_five=5 if _n>=_N/5*4

*return rank
gen ret_five=.
sort monthyear ret_5
by monthyear: replace ret_five=1 if _n<_N/5
by monthyear: replace ret_five=2 if _n>=_N/5 & _n<_N/5*2
by monthyear: replace ret_five=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace ret_five=4 if _n>=_N/5*3 & _n<_N/5*4
by monthyear: replace ret_five=5 if _n>=_N/5*4

egen convex_ret=group(convex_five ret_five)

replace futret_5=futret_5*100

egen vw_raw=wtmean(futret_5), weight(mktcap) by(monthyear convex_ret)

sort convex_ret monthyear 
collapse vw_raw, by(convex_ret monthyear)

tsset convex_ret monthyear  

**FIRST 5 x 5 PORTFOLIOS
forval i=1/25 {
	eststo: newey vw_raw if convex_ret ==`i', lag(6)
}

**LAST COLUMN
sort monthyear convex_ret
keep if convex_ret==1 | convex_ret==5 | convex_ret==6 | convex_ret==10 | convex_ret==11 | convex_ret==15 | convex_ret==16 | convex_ret==20 | convex_ret==21 | convex_ret==25

gen vw_raw_dif=vw_raw-vw_raw[_n+1] if convex_ret==1 | convex_ret==6 | convex_ret==11 | convex_ret==16 | convex_ret==21

sort convex_ret monthyear 
foreach i in 1 6 11 16 21 { 
	eststo: newey vw_raw_dif if convex_ret ==`i', lag(6)
}
}


****LAST ROW***
use "$jfqa_rep/20_Stock_Setup.dta", clear

qui{
foreach i in ret_5 futret_5 five_day_convex_split {
	drop if missing(`i')
}

drop if year<1963
drop if year==1963 & month<6

drop if year>2022
drop if year==2022 & month==12

*convexity split rank
gen convex_five=.
sort monthyear five_day_convex_split
by monthyear: replace convex_five=1 if _n<_N/5
by monthyear: replace convex_five=2 if _n>=_N/5 & _n<_N/5*2
by monthyear: replace convex_five=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace convex_five=4 if _n>=_N/5*3 & _n<_N/5*4
by monthyear: replace convex_five=5 if _n>=_N/5*4

*return rank
gen ret_five=.
sort monthyear ret_5
by monthyear: replace ret_five=1 if _n<_N/5
by monthyear: replace ret_five=2 if _n>=_N/5 & _n<_N/5*2
by monthyear: replace ret_five=3 if _n>=_N/5*2 & _n<_N/5*3 
by monthyear: replace ret_five=4 if _n>=_N/5*3 & _n<_N/5*4
by monthyear: replace ret_five=5 if _n>=_N/5*4

egen convex_ret=group(ret_five convex_five)

replace futret_5=futret_5*100

egen vw_raw=wtmean(futret_5), weight(mktcap) by(monthyear convex_ret)

collapse vw_raw, by(convex_ret monthyear)

sort monthyear convex_ret

keep if convex_ret==1 | convex_ret==5 | convex_ret==6 | convex_ret==10 | convex_ret==11 | convex_ret==15 | convex_ret==16 | convex_ret==20 | convex_ret==21 | convex_ret==25

*average difference returns
gen vw_raw_dif=vw_raw-vw_raw[_n+1] if convex_ret==1 | convex_ret==6 | convex_ret==11 | convex_ret==16 | convex_ret==21

sort  convex_ret monthyear
tsset convex_ret monthyear
foreach i in 1 6 11 16 21 { 
	eststo: newey vw_raw_dif if convex_ret ==`i', lag(6)
}
}

esttab using "$jfqa_rep/Table_3C.csv", nostar replace b(2)


